import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import routes from './routes'
import tools from '../utils/tools'

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 白名单<不需要权限即可进入>
const whitelist = ['/login']


// // 全局前置守卫
// eslint-disable-next-line consistent-return, no-unused-vars
router.beforeEach((to, from) => {
    // console.log(from)
    if (!whitelist.includes(to.path)) {
      const token = tools.getCookie('token')
      if (!token) {
        ElMessage.error('登录过期')
        return '/login'
      }
    }
  })
  // 添加全局后置守卫
  router.afterEach((to, from) => {
    const title = (to.meta.title) || 'OA' // 获取路由配置中的 title 字段或默认标题
    document.title = title // 设置页面标题
  })
  
  export default router